<?php


namespace App\Models\CacheModel\live;


use think\Log;
use AstroTool\Cache\CacheManage;

class LiveAnswerDiceRecord extends LiveAnswerBaseModel
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_answer_dice_record';

    protected $cacheModel = 'live.dice_answer';

    protected $modelType = 'dice';
    protected $modelTypeNeedAuth = 2;

    /**
     * 新增   骰子申请
     * @param array $data
     * $data = [
     *      'type' => 1单盘  2双盘,
     *      'live_id' => 直播间ID,
     *      'live_record_id' => 直播记录ID,
     *      'uid' => $uid,
     *      'price' => 价格 0为免费,
     *      'rid' => $rid,
     *      'rid2' => $type === 2 ? $rid2 : 0,
     *      'question' => 看盘问题,
     *      'question_des' => 看盘问题描述
     *      'extend'=>[recode1=>档案1  recode2=>档案2]
     *      'order_num'=> 订单号
     * ];
     * @return int
     */
    public function addAnswerRequest(array $data)
    {
        try
        {
            $insertData = [
                'uid' => $data['uid'],
                'live_id' => $data['live_id'],
                'live_record_id' => $data['live_record_id'],
                'history_id' => $data['history_id'],
                'price' => $data['price'],
                'question' => $data['question'],
                'evaluate_status' => 0,
                'reward_status' => 0,
                'status' => $data['price'] === 0 ? 2 : 1,   // 状态  1=>待支付 2=>等待解答｜排队中  3=>待响应 4=>未响应 5=>解答中 6=>解答完成 7=>超时取消 8=>用户主动取消 9=>主播取消
                'create_time' => time(),
            ];

            $id = $this->insertGetId($insertData);
            if ($insertData['status'] === 2)
            {   // 等待解答中  加入到待解答记录中 免费
                CacheManage::getInstance()
                    ->setCacheConfig($this->cacheModel, $data['live_id'] . ':' . $data['live_record_id'])
                    ->listAdd([$data['uid'] => $id]);
            } else
            {   // 付费
                $this->setUserRequestAnswerPrepareInfo($data['live_id'], $data['live_record_id'], $data['uid'], ['status' => 1, 'id' => $id]);
            }

            return $id;
        }
        catch (\Exception $e)
        {
            Log::write("申请 看盘 写入数据失败{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()};", 'error');
            return 0;
        }
    }
}
